<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../jquery.min.js"></script>
<script type="text/javascript" src="../jquery.easyui.min.js"></script>
<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>

<script src="../js/xheditor/xheditor-1.2.2.min.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../js/jqurey.parseurl.min.js"></script>

<body>
	<div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">

        	<table id="dg" data-options="toolbar:'#commentRem'"></table>

        	<div class="wu-toolbar-button" id="commentRem">       		           
	           <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeComment()" plain="true">选中删除</a>
	           <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-add" onclick="addComment()" plain="true">添加评论</a> 
	        </div>
        </div>
    </div>
    
    
    <div id="comment-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
    	 <form id="comment-form" class="easyui-form" method="post" data-options="novalidate:true">
		    <div style="margin-bottom:20px">		    	
		       评论内容：<textarea name="content" rows="30" cols="60" class="xheditor" data-options="required:true"></textarea>	
		    </div>
	 	</form> 
	</div>
    
    <script>
		var nodeId = null;
		function initList(newsId){
			//console.log(cateId);
			var postUrl;
			if(newsId !=undefined && newsId.trim().length>0){
				queryParams = {newsId: newsId}
			}else{
				queryParams = {};
			}
			$("#dg").datagrid({
				url:'http://localhost:3000/comment/list',
				method:'post',
				pagination:true,
				pageSize:2,
				queryParams: queryParams,
				pageList:[2,4,6,8,10],
				fit:true,
				columns:[[
			        {field:'check',title:'check',width:100,checkbox:true},
			        {field:'userId',title:'用户',width:170},
			        {field:'content',title:'评论内容',width:180},
			        {field:'newsId',title:'所属新闻',width:170},
			        {field:'date',title:'评论时间',width:180},
			        {field:'operate',title:'在线管理', width:150,
						formatter: function(value,row,index){
							return '<a href="javascript:void(0)" onclick="editComment(\''+ row._id+'\')" class="editcls">编辑</a>'+
							'<a href="javascript:void(0)" onclick="deleComment(\''+ row._id+'\')" class="removecls">删除</a>'
						}
					}
			    ]],
			    onLoadSuccess:function(data){  
	                $('.editcls').linkbutton({text:'修改',plain:true,iconCls:'icon-edit'}); 
	                $('.removecls').linkbutton({text:'删除',plain:true,iconCls:'icon-remove'});
	            }
			})		
		}
		
		initList();
		
		

		function editComment(id){//修改
			nodeId = id;
			var obj = $('#dg').datagrid('getSelected');
			$('#comment-form').form('clear');
			$("#comment-dialog").dialog({
				closed: false,
				modal:true,
		        title: "修改评论内容",
		        onOpen:function(){
		        	$("#comment-form").form('load',obj);
		        },
		        buttons: [{
		            text: '确定',
		            iconCls: 'icon-ok',
		            handler: commentEditAction
		        }, {
		            text: '取消',
		            iconCls: 'icon-cancel',
		            handler: function () {
		                $('#comment-dialog').dialog('close');                    
		            }
		        }]
		    });
		}
		
		function commentEditAction(){
			$('#comment-dialog').form('submit',{
		        onSubmit:function(){
		            if ($(this).form('enableValidation').form('validate')){
						$.ajax({
							type:"post",
							url:"http://localhost:3000/comment/data/"+nodeId,
							data:$("#comment-form").serializeJSON()
						}).then(function(res){							
							$('#dg').datagrid('reload');
						});
		            }else{
		            	alert('no');
		            }
		            $('#comment-dialog').dialog('close');					
		        }
		    });
		}

		function deleComment(id){//删除			
			$.ajax({
				type:"delete",
				url:"http://localhost:3000/comment/data/"+ id
			}).then(function(res){		
				$('#dg').datagrid('reload');
			});
		}

		//删除选中
		function removeComment(id){
			var objs = $('#dg').datagrid('getSelections');
			//console.log(objs);
			for(var i = 0;i<objs.length;i++){
				deleComment(objs[i]._id);
			}
		}
		


		//添加
		var newsId = $.parseUrl(window.location.href).query.id;
		var userId = null;
		
		initList(newsId);

		function addComment(){
			var shishi = getCookie('newsId');
			console.log(shishi);		
			$('#comment-form').form('clear');
			$("#comment-dialog").dialog({
				closed: false,
				modal:true,
		        title: "添加评论内容",
		        buttons: [{
		            text: '确定',
		            iconCls: 'icon-ok',
		            handler: commentAddAction
		        }, {
		            text: '取消',
		            iconCls: 'icon-cancel',
		            handler: function () {
		                $('#comment-dialog').dialog('close');                    
		            }
		        }]
		    });
		}
		
		function commentAddAction(){
			var addata = $("#comment-form").serializeJSON();
			console.log(addata);
			if(newsId){
				addata.newsId = newsId;
			}else{
				addata.newsId = '59cb711bdfcf072db4a04792';
				//alert('请选择新闻');
			}

			if(userId){
				addata.userId = userId;				
			}else{
				addata.userId = '59cb4b8094887f36000d732b';	
			}
			
			$('#comment-dialog').form('submit',{
		        onSubmit:function(){
		            if ($(this).form('enableValidation').form('validate')){
		            	//console.log(nodeId);
						$.ajax({
							type:"post",
							url:"http://localhost:3000/comment/data",
							data:addata
						}).then(function(res){					
							$('#dg').datagrid('reload');
						});
		            }else{
		            	alert('no');
		            }
		            $('#comment-dialog').dialog('close');					
		        }
		    });
		}

		
	</script>
</body>
</html>
